package com.atguigu.lease.common.exception;

import com.atguigu.lease.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Package: com.atguigu.lease.common.exception
 *
 * @Description: 全局异常处理器
 * @Author xiongkunYang
 * @Create 2025/4/25 14:04
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 自定义业务异常处理器
     */
    @ExceptionHandler(LeaseException.class)
    public Result<Void> error(LeaseException e){
        log.error("Error message: {}, Cause: {}", e.getMessage(), e.getCause(), e);
        return Result.fail(e.getCode(), e.getMessage());
    }


    /**
     * 全局异常处理器
     */
    @ExceptionHandler(Exception.class)
    public Result<Void> exceptionHandler(Exception e) {
        log.error("Error message: {}, Cause: {}", e.getMessage(), e.getCause(), e);
        return Result.fail();
    }
}